package POI;

import libgdx.Shape;
import util.LowPassFilter;
import util.OBJLoader;
import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLUtils;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g3d.model.still.StillModel;

public class POI extends Shape {
	StillModel body;
	String intersectedMesh;
	private Texture textureBody;
	private String id;

	public POI(Context context, boolean willBeDrawed, String id) {
		super(context);
		this.id = id;
		if (willBeDrawed) {
			body = new StillModel(OBJLoader.POIBODY.getSubMeshes());
			filter = new LowPassFilter();
		}

	}

	

	public void dispose() {
		try {
			textureBody.dispose();
			textureBody = null;
			body.dispose();
		} catch (Exception e) {

		}
	}

	public void setBodyTexture() {
		addTexture("", true);
	}

	public void addTexture(String file, boolean gen) {

		textureBody = new Texture(Gdx.files.internal(file));
		textureBody.setFilter(TextureFilter.Linear, TextureFilter.Linear);

	}

	public static int loadGLTextureFromBitmap(Bitmap bitmap) {

		// Generate one texture pointer
		int[] textureIds = new int[1];
		Gdx.gl10.glGenTextures(1, textureIds, 0);

		// bind this texture
		Gdx.gl10.glBindTexture(GL10.GL_TEXTURE_2D, textureIds[0]);

		// Create Nearest Filtered Texture
		Gdx.gl10.glTexParameterf(GL10.GL_TEXTURE_2D,
				GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
		Gdx.gl10.glTexParameterf(GL10.GL_TEXTURE_2D,
				GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

		Gdx.gl10.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
				GL10.GL_REPEAT);
		Gdx.gl10.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
				GL10.GL_REPEAT);

		// Use the Android GLUtils to specify a two-dimensional texture image
		// from our bitmap
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

		return textureIds[0];
	}

	public void render() {

		Gdx.gl10.glPushMatrix();
		float[] infilter = { virX, virY, virZ };
		float[] outfilter = this.filter.lowPass(infilter, 0.06f);
		Gdx.gl10.glTranslatef(outfilter[0], outfilter[1], outfilter[2]);
		angle = (float) Math.atan(outfilter[1] / outfilter[0]);
		showX = outfilter[0];
		showY = outfilter[1];
		showZ = outfilter[2];
		xAxis = 0;
		yAxis = 0;
		zAxis = 1;
		angle = showX > 0 && showY > 0 || showX > 0 && showY < 0 ? (float) (angle * 180 / Math.PI)
				: 180 + (float) (angle * 180 / Math.PI);
		Gdx.gl10.glRotatef(angle, xAxis, yAxis, zAxis);
		Gdx.gl10.glScalef(0.25f, 0.25f, 0.25f);
		textureBody.bind();
		body.subMeshes[0].mesh.render(GL10.GL_TRIANGLES);
		Gdx.gl10.glBindTexture(GL10.GL_TEXTURE_2D, 0);
		Gdx.gl10.glPopMatrix();
	}

	// public void renderForPicking() {
	// Gdx.gl10.glPushMatrix();
	// Gdx.gl10.glEnable(GL10.GL_COLOR_MATERIAL);
	// Gdx.gl10.glDisable(GL10.GL_TEXTURE_2D);
	// float[] infilter = { virX, virY, virZ };
	// float[] outfilter = this.filter.lowPass(infilter, 0.06f);
	// Gdx.gl10.glTranslatef(outfilter[0], outfilter[1], outfilter[2]);
	// angle = (float) Math.atan(outfilter[1] / outfilter[0]);
	// showX = outfilter[0];
	// showY = outfilter[1];
	// showZ = outfilter[2];
	// xAxis = 0;
	// yAxis = 0;
	// zAxis = 1;
	// angle = showX > 0 && showY > 0 || showX > 0 && showY < 0 ? (float) (angle
	// * 180 / Math.PI)
	// : 180 + (float) (angle * 180 / Math.PI);
	// Gdx.gl10.glRotatef(angle, xAxis, yAxis, zAxis);
	// Gdx.gl10.glScalef(scaleX, scaleY, scaleZ);
	// Gdx.gl11.glColor4ub((byte) pickingColor.R, (byte) pickingColor.G,
	// (byte) pickingColor.B, (byte) 255);
	// body.subMeshes[0].mesh.render(GL10.GL_TRIANGLES);
	// Gdx.gl10.glEnable(GL10.GL_TEXTURE_2D);
	// Gdx.gl10.glEnable(GL10.GL_DITHER);
	// Gdx.gl10.glDisable(GL10.GL_COLOR_MATERIAL);
	// Gdx.gl10.glPopMatrix();
	//
	// }

	public void caclulateRadius() {
		radius = (float) Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)
				+ Math.pow(z, 2));
	}

	public void caclulateShowRadius() {
		showRadius = (float) Math.sqrt(Math.pow(showX, 2) + Math.pow(showY, 2)
				+ Math.pow(showZ, 2));
	}

	public String getID() {
		return id;
	}

	public Texture getTextureBody() {
		return textureBody;
	}

	@Override
	public void onLocationChanged(double[] location) {
		this.setTranslationMatrix((float) location[0], (float) location[1], 0);
		this.radarPOI.onLocationChanged(location);

	}

}